import { ref, reactive, onMounted } from 'vue';
import { getRdsUrl, rdsRequestAsync } from './rdsServices';
import type { tb_id_pro_type }  from '../pages/RdsViews/entities/tb_id_pro_type';

export function useProTypeHook() {
    const rdsUrl = getRdsUrl();

    const proTypes = ref([] as tb_id_pro_type[]);
    const proTypeItems = ref([] as { value: string; title: string; }[]);

    const initProTypes = async () => {
        const result = await rdsRequestAsync(rdsUrl.bcApi, '促销类型定义', 'GetAllProType', '');
        if (!result.HasException) {
            proTypes.value = result.ObjectData;
            updateProTypeItems();
        }
    };

    const getProTypeName = (id?: string) => {
        const item = proTypes.value.find(g => g.c_pro_type === id);
        return item?.c_name;
    };

    const getProType = (id?: string) => {
        const item = proTypes.value.find(g => g.c_pro_type === id);
        return item;
    };

    const updateProTypeItems = () => {
        proTypeItems.value = getProTypeItems();
    };

    const getProTypeItems = (props?: { kind?: string[], exclude_kind?: string[] }) => {
        const { kind, exclude_kind = [] } = props || {};
        return proTypes.value.filter(a =>
            (!kind || kind.includes(<string>a.c_kind))
        ).filter(a =>
            (!exclude_kind.includes(<string>a.c_kind))
        ).map(item => ({
            value: item.c_pro_type,
            title: item.c_name
        }));
    };

    onMounted(async () => {
       await initProTypes();
    });

    return { proTypeItems, getProTypeName, getProType, getProTypeItems };
}
